home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / dix / RCS / atom.c,v < prev    next >
Encoding:
Text File  |  1990-02-14  |  4.5 KB  |  209 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     90.02.14.15.23.10;  author tve;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @Original X11R4 distribution
  17. @
  18.  
  19.  
  20.  
  21. 1.1
  22. log
  23. @Initial revision
  24. @
  25. text
  26. @/***********************************************************
  27. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  28. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  29.  
  30.                         All Rights Reserved
  31.  
  32. Permission to use, copy, modify, and distribute this software and its 
  33. documentation for any purpose and without fee is hereby granted, 
  34. provided that the above copyright notice appear in all copies and that
  35. both that copyright notice and this permission notice appear in 
  36. supporting documentation, and that the names of Digital or MIT not be
  37. used in advertising or publicity pertaining to distribution of the
  38. software without specific, written prior permission.  
  39.  
  40. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  41. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  42. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  43. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  44. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  45. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  46. SOFTWARE.
  47.  
  48. ******************************************************************/
  49.  
  50. /* $XConsortium: atom.c,v 1.28 89/08/02 09:10:50 rws Exp $ */
  51.  
  52. #include "X.h"
  53. #include "Xatom.h"
  54. #include "misc.h"
  55. #include "resource.h"
  56.  
  57. #define InitialTableSize 100
  58.  
  59. typedef struct _Node {
  60.     struct _Node   *left,   *right;
  61.     Atom a;
  62.     unsigned int fingerPrint;
  63.     char   *string;
  64. } NodeRec, *NodePtr;
  65.  
  66. static Atom lastAtom = None;
  67. static NodePtr atomRoot = (NodePtr)NULL;
  68. static unsigned long tableLength;
  69. static NodePtr *nodeTable;
  70.  
  71. Atom 
  72. MakeAtom(string, len, makeit)
  73.     char *string;
  74.     unsigned len;
  75.     Bool makeit;
  76. {
  77.     register    NodePtr * np;
  78.     unsigned i;
  79.     int     comp;
  80.     register unsigned int   fp = 0;
  81.  
  82.     np = &atomRoot;
  83.     for (i = 0; i < (len+1)/2; i++)
  84.     {
  85.     fp = fp * 27 + string[i];
  86.     fp = fp * 27 + string[len - 1 - i];
  87.     }
  88.     while (*np != (NodePtr) NULL)
  89.     {
  90.     if (fp < (*np)->fingerPrint)
  91.         np = &((*np)->left);
  92.     else if (fp > (*np)->fingerPrint)
  93.         np = &((*np)->right);
  94.     else
  95.     {                   /* now start testing the strings */
  96.         comp = strncmp(string, (*np)->string, (int)len);
  97.         if ((comp < 0) || ((comp == 0) && (len < strlen((*np)->string))))
  98.         np = &((*np)->left);
  99.         else if (comp > 0)
  100.         np = &((*np)->right);
  101.         else
  102.         return(*np)->a;
  103.         }
  104.     }
  105.     if (makeit)
  106.     {
  107.     register NodePtr nd;
  108.  
  109.     nd = (NodePtr) xalloc(sizeof(NodeRec));
  110.     if (!nd)
  111.         return BAD_RESOURCE;
  112.     if (lastAtom < XA_LAST_PREDEFINED)
  113.     {
  114.         nd->string = string;
  115.     }
  116.     else
  117.     {
  118.         nd->string = (char *) xalloc(len + 1);
  119.         if (!nd->string) {
  120.         xfree(nd);
  121.         return BAD_RESOURCE;
  122.         }
  123.         strncpy(nd->string, string, (int)len);
  124.         nd->string[len] = 0;
  125.     }
  126.     if ((lastAtom + 1) >= tableLength) {
  127.         NodePtr *table;
  128.  
  129.         table = (NodePtr *) xrealloc(nodeTable,
  130.                      tableLength * (2 * sizeof(NodePtr)));
  131.         if (!table) {
  132.         if (nd->string != string)
  133.             xfree(nd->string);
  134.         xfree(nd);
  135.         return BAD_RESOURCE;
  136.         }
  137.         tableLength <<= 1;
  138.         nodeTable = table;
  139.     }
  140.     *np = nd;
  141.     nd->left = nd->right = (NodePtr) NULL;
  142.     nd->fingerPrint = fp;
  143.     nd->a = (++lastAtom);
  144.     *(nodeTable+lastAtom) = nd;
  145.     return nd->a;
  146.     }
  147.     else
  148.     return None;
  149. }
  150.  
  151. ValidAtom(atom)
  152.     Atom atom;
  153. {
  154.     return (atom != None) && (atom <= lastAtom);
  155. }
  156.  
  157. char *
  158. NameForAtom(atom)
  159.     Atom atom;
  160. {
  161.     NodePtr node;
  162.     if (atom > lastAtom) return 0;
  163.     if ((node = nodeTable[atom]) == (NodePtr)NULL) return 0;
  164.     return node->string;
  165. }
  166.  
  167. AtomError()
  168. {
  169.     FatalError("initializing atoms");
  170. }
  171.  
  172. InitAtoms()
  173. {
  174.     FreeAllAtoms();
  175.     tableLength = InitialTableSize;
  176.     nodeTable = (NodePtr *)xalloc(InitialTableSize*sizeof(NodePtr));
  177.     if (!nodeTable)
  178.     AtomError();
  179.     nodeTable[None] = (NodePtr)NULL;
  180.     MakePredeclaredAtoms();
  181.     if (lastAtom != XA_LAST_PREDEFINED)
  182.     AtomError ();
  183. }
  184.  
  185. FreeAllAtoms()
  186. {
  187.     if(atomRoot == (NodePtr)NULL)
  188.     return;
  189.     FreeAtom(atomRoot);
  190.     atomRoot = (NodePtr)NULL;
  191.     xfree(nodeTable);
  192.     nodeTable = (NodePtr *)NULL;
  193.     lastAtom = None;
  194. }
  195.  
  196. FreeAtom(patom)
  197.     NodePtr patom;
  198. {
  199.     if(patom->left)
  200.     FreeAtom(patom->left);
  201.     if(patom->right)
  202.     FreeAtom(patom->right);
  203.     if (patom->a > XA_LAST_PREDEFINED)
  204.     xfree(patom->string);
  205.     xfree(patom);
  206. }
  207.     
  208. @
  209.